package com.jdz.gxq.info.admin.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jdz.gxq.info.admin.project.bo.Project;
import com.jdz.gxq.info.admin.project.bo.YearInspection;
import com.jdz.gxq.info.admin.project.form.InspectionForm;
import com.jdz.gxq.info.admin.project.service.InspectionService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class InspectionAction extends CommonDispatchAction {

	private InspectionService inspectionService = null;
	 
	public ActionForward projectList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		InspectionForm inspectionForm = (InspectionForm) form;
		
		String projectName = inspectionForm.getProjectName();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("projectName", projectName);
		
		int totalCount = inspectionService.getInspectionProjectsCount(params);
		
		int currentPage = inspectionForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);
		
		inspectionForm.setPageIndex(pageBean.getCurrentPage());

		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());

		List<Project> projectList = inspectionService.getInspectionProjects(params);
		
		request.setAttribute("projectList", projectList);
		request.setAttribute("pageBean", pageBean);
		
		return mapping.findForward("success");
	}
	
	public ActionForward inspectionList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		InspectionForm inspectionForm = (InspectionForm) form;
		
		int projectId = inspectionForm.getId();
	
		Project project = inspectionService.getProject(projectId);
		
		List<YearInspection> yearInspectList = inspectionService.getInspectionList(projectId);
		
		request.setAttribute("project", project);
		
		request.setAttribute("yearInspectList", yearInspectList);

		return mapping.findForward("success");
	}

	public void setInspectionService(InspectionService inspectionService) {
		this.inspectionService = inspectionService;
	}
}

